#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

/* 
 * the errno is a macro
 * man fopen : 
 * #include <stdio.h>
 *
 * FILE *fopen(const char *pathname, const char *mode);
 * FILE *fdopen(int fd, const char *mode);
 * FILE *freopen(const char *pathname, const char *mode, FILE *stream);
 *
 * compile : gcc max_fopen.c -o max_fopen
 * run : fopen(): Too many open files
 * 	 count = 1021
 *
 * the nums of one process can open is (1021 + stdin stdout stderr)
 * ulimit -a
 */
int main()
{
	FILE *fp = NULL;
	int count = 0;
	while (1) {
		if ((fp = fopen("tmp", "w")) == NULL) {
			perror("fopen()");
			break;
		}
		count++;
	}
	printf("count = %d\n", count);

	exit(0);
}
